/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 *
 * fileName:  DemoAction.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-15 下午3:03:32
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-15          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.webapp.action.demo;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cloudland.web.multipart.MultiActionControllerAdapter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * <pre>
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 {@link java.lang.Object#toString()}, 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * 
 * @ClassName DemoAction
 * @author lei
 * @version %I%, %G%
 * @see
 * @since JDK1.6
 * 
 */
@Controller
@RequestMapping("/demo/*")
public class DemoAnnoationAction extends MultiActionControllerAdapter {

	@RequestMapping(value = "example", method = RequestMethod.GET)
	public String list(HttpServletRequest request, HttpServletResponse response) {
		getLogger().info("跳转至样例目录");
		return "/demo/example";
	}

	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public void doUpload(HttpServletRequest request, HttpServletResponse response) {
		getLogger().info("上传文件");
		
		String type = request.getParameter("dir");
		String id = request.getParameter("id");
		
		// 获取当前请求对象, 并判断是否是上传请求
		if (!(request instanceof MultipartHttpServletRequest)) {
			// 上传请求的enctype类型不正确，无法正确获取上传文件
			return;
		}
				
		// 获取上传文件集合
		List<MultipartFile> list = ((MultipartHttpServletRequest) request).getFiles("imgFile");
		
		StringBuffer buff = new StringBuffer("type: ").append(type).append("id: ").append(id);
		for (MultipartFile uploadFile : list) {
			// 判断上传文件是否为空
			if  (uploadFile.isEmpty()) {
				continue;
			}
			
			buff.append("-------\n");
			buff.append(", OriginalFilename: ").append(uploadFile.getOriginalFilename());
			buff.append(", name: ").append(uploadFile.getName());
		}
		
		getLogger().info(buff);
		
		try {
			PrintWriter out = response.getWriter();
			out.print("{\"error\" : 0, \"url\" : \"http://www.example.com/path/to/file.ext\"}");
			out.flush();
			out.close();
		} catch (Exception e) {
			
		}
	}

	@RequestMapping(value = "{path}", method = RequestMethod.GET)
	public String doPath(@PathVariable String path, HttpServletRequest request,
			HttpServletResponse response) {
		getLogger().info("跳转至样例目录");
		return "/demo/" + path;
	}
}
